//
//  BBViewController.m
//  BBTouch
//
//  Created by ben smith on 8/13/08.
//  This file is part of BBTouch.
//
//  BBTouch is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  BBTouch is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.

//  You should have received a copy of the GNU Lesser General Public License
//  along with BBTouch.  If not, see <http://www.gnu.org/licenses/>.
// 
//  Copyright 2008 Ben Britten Smith ben@benbritten.com .
//

#import "BBViewController.h"
#import "BBEscapeWindow.h"

#import "BBScreenSelectionView.h"
#import "BBCompositeView.h"
#import "BBConfigurationController.h"


@implementation BBViewController

@synthesize borrowedView;

-(void)awakeFromNib
{

}

-(IBAction)goFullScreen:(id)sender;
{
	int windowLevel;
	
	NSNumber * defaultNum =	[BBConfigurationController objectForSettingKey:@"BBDefaultScreenNumber"];
	NSScreen * chosenScreen = [BBScreenSelectionView screenWithNumber:defaultNum];
		
  CGDirectDisplayID displayID = [defaultNum intValue];
	if (CGDisplayCapture( displayID ) != kCGErrorSuccess) {
		//	if (CGCaptureAllDisplays() != kCGErrorSuccess) {
		NSLog( @"Couldn't capture the display!" );
	}
	
	// Get the shielding window level
	windowLevel = CGShieldingWindowLevel();
	// Get the screen rect of our main display
	screenRect = [chosenScreen frame];
	// Put up a new window
	fullScreenWindow = [[BBEscapeWindow alloc] initWithContentRect:screenRect
																											 styleMask:NSBorderlessWindowMask
																												 backing:NSBackingStoreBuffered
																													 defer:NO
																													screen:chosenScreen];
	[fullScreenWindow setLevel:windowLevel];
	[fullScreenWindow setFrame:screenRect display:YES]; // do this twice for some reason, otherwise you sometimes get the wrong screen rect
	[fullScreenWindow setBackgroundColor:[NSColor blackColor]];
	[fullScreenWindow makeKeyAndOrderFront:nil];
	
	// now get a view for our new big window
	NSView * bigView = [compositeView currentView];
	[compositeView borrowView:bigView];
	[self setBorrowedView:bigView];
	// step 1 calibrate the project frame
	[bigView setFrame:screenRect];
	[fullScreenWindow setContentView:bigView];
	[[self.borrowedView window] makeFirstResponder:self.borrowedView];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closeFullScreenWindow:) name:@"BBEscapeKeyDownNotification" object:nil];
}

-(IBAction)closeFullScreenWindow:(id)sender
{
	if (fullScreenWindow == nil) return;
	
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"BBEscapeKeyDownNotification" object:nil];
	
	[fullScreenWindow orderOut:self];
	[fullScreenWindow release];
	// Release the display(s)
	//CGDirectDisplayID displayID = [defaultNum intValue];
	//if (CGDisplayRelease( displayID ) != kCGErrorSuccess) {
	
	if (CGReleaseAllDisplays() != kCGErrorSuccess) {
		// need to make this a bit more friendly
		NSLog( @"Couldn't release the display(s)!" );
	}

	[[self.borrowedView window] makeFirstResponder:self.borrowedView];
	[compositeView returnView:self.borrowedView];

}


@end
